import Vue from 'vue'
import VueRouter from 'vue-router'
import TheFilms from '@/views/TheFilms'
import NowPlaying from '@/views/films/NowPlaying'
import ComingSoon from '@/views/films/ComingSoon'
import TheCity from '@/views/TheCity'
import TheCinemas from '@/views/TheCinemas'
import TheSearch from '@/views/TheSearch'
import TheCinema from '@/views/TheCinema'
import TheFilmData from '@/views/cinema/TheFilmData'

// 解决路由重复报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    // 电影
    path: '/films',
    component: TheFilms,
    children: [
      {
        path: '/films/nowplaying',
        component: NowPlaying
      },
      {
        path: '/films/comingsoon',
        component: ComingSoon
      },
      {
        path: '/films',
        redirect: '/films/nowplaying'
      }
    ]
  },
  {
    // 电影详情
    path: '/detail/:id',
    component: () => import('@/views/TheDetail')
  },
  {
    // 影院
    path: '/cinemas',
    component: TheCinemas
  },
  {
    // 影院详情
    path: '/cinema/:cinemaId',
    component: TheCinema,
    children: [
      {
        path: '/cinema/:cinemaId/film',
        component: TheFilmData
      },
      {
        path: '/cinema/:cinemaId/film/:filmId',
        component: TheFilmData
      },
      {
        path: '/cinema/:cinemaId/film/:filmId/:showDateId',
        component: TheFilmData
      },
      {
        path: '',
        redirect: '/cinema/:cinemaId/film'
      }
    ]
  },
  {
    // 影院搜索
    path: '/cinemas/search',
    component: TheSearch
  },
  {
    // 城市
    path: '/city',
    component: TheCity
  },
  {
    // 我的
    path: '/center',
    component: () => import('@/views/TheCenter')
  },
  {
    path: '*',
    redirect: '/films'
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

export default router
